package com.kichina.parking.controller.parking.handler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kichina.parking.service.logic.bean.YardStatusInfo;
import com.kichina.parking.service.logic.service.YardStatusInfoService;
import com.kichina.parking.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author Surging
 * @version 1.0
 * @ClassName YardStatusInfoHandler
 * @date 2022-10-09 16:34
 * @desc 车场在线状态处理
 */
@Component
@Slf4j
public class YardStatusInfoHandler {

    private final YardStatusInfoService yardStatusInfoService;

    public YardStatusInfoHandler(YardStatusInfoService yardStatusInfoService) {
        this.yardStatusInfoService = yardStatusInfoService;
    }

    /**
     * @param yardStatusInfo 车场信息实体
     * @author Surging
     * @Description 添加或更新车场在线状态
     * @Return void
     * @date 16:46 2022-10-09
     **/
    public void saveOrUpdateYardInfo(YardStatusInfo yardStatusInfo) {
        YardStatusInfo statusInfo = yardStatusInfoService.getOne(new QueryWrapper<YardStatusInfo>().lambda()
                .eq(YardStatusInfo::getYardKey, yardStatusInfo.getYardKey())
                .last("limit 1"));
        if (Objects.nonNull(statusInfo)){
            statusInfo.setUpdateTime(new Date());
            statusInfo.setIsOnline(yardStatusInfo.getIsOnline());
            yardStatusInfoService.updateById(statusInfo);
        }else {
            yardStatusInfo.setCreateTime(new Date());
            yardStatusInfo.setUpdateTime(new Date());
            yardStatusInfo.setYardStatusId(UUIDUtil.getUUID());
            yardStatusInfoService.save(yardStatusInfo);
        }
    }

    /**
     * @author Surging
     * @Description 获取在线的所有车场信息
     * @Return java.util.List<YardStatusInfo>
     * @date 17:28 2022-10-09
     **/
    public List<YardStatusInfo> getYardStatusInfoList() {
        return yardStatusInfoService.list(new QueryWrapper<YardStatusInfo>().lambda()
                .eq(YardStatusInfo::getIsOnline, true));
    }
}
